package frist50;

import org.junit.jupiter.api.Test;

import java.util.Deque;
import java.util.LinkedList;

public class T0038 {
    public String countAndSay(int n) {
        Deque<Integer> deque=new LinkedList<>();
        deque.add(1);
        for (int i = 0; i <n-1 ; i++) {
            int num = deque.poll();
            int sum=1;
            int size=deque.size();
            for (int j = 0; j <size ; j++) {
                int t=deque.poll();
                if (t==num){
                    sum++;
                }else {
                    deque.add(sum);
                    deque.add(num);
                    sum=1;
                    num=t;
                }
            }
            deque.add(sum);
            deque.add(num);
        }
        String str="";
        while (!deque.isEmpty()){
            str=str+deque.poll();
        }
        return str;
    }

    @Test
    public void test() {

        System.out.println(countAndSay(5));
    }
}
